﻿using System.Windows.Input;
using System;
using System.Windows.Media;
using System.Threading;

namespace KinectCurve.Entities
{
    /// <summary>
    /// Player class
    /// </summary>
    public class Player
    {
        private Random randomgenerator;
        private int randomSeed = Environment.TickCount;
        private int _playerNumber = 0;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="playerNumber"></param>
        public Player(int playerNumber)
        {
            if (playerNumber < 1 || playerNumber > 10) throw new ArgumentException("Player number is invalid!", "playerNumber");

            Thread.Sleep(20); //because of random
            randomgenerator = new Random(randomSeed++);

            _playerNumber = playerNumber;
            Angle = randomgenerator.Next(0, 360);
        }

        /// <summary>
        /// Player name
        /// </summary>
        public string PlayerName { get { return string.Format("Player{0}", _playerNumber.ToString()); } }

        /// <summary>
        /// Mapping for Left direction
        /// </summary>
        public Key LeftKey { get; set; } //we won't need this with Kinect

        /// <summary>
        /// Mapping for Right direction
        /// </summary>
        public Key RightKey { get; set; } //we won't need this with Kinect

        /// <summary>
        /// Score of player
        /// </summary>
        public int Score { get; set; }

        /// <summary>
        /// Point collection of player
        /// </summary>
        public PointCollection CoordinatePoints { get; set; }

        /// <summary>
        /// X coordinate of player
        /// </summary>
        public int X { get; set; }

        /// <summary>
        /// Y coordinate of player
        /// </summary>
        public int Y { get; set; }

        /// <summary>
        /// If player is alive or dead
        /// </summary>
        public bool IsAlive { get; set; }

        /// <summary>
        /// Angle - in which direction the player is moving
        /// </summary>
        public double Angle { get; set; }

        /// <summary>
        /// Method that generates new coordinates for the player depending on the angle and speed
        /// </summary>
        /// <param name="key"></param>
        public void Move(Key key)
        {
            if (RightKey == key) Angle -= Settings.Radius * Settings.Speed;
            if (LeftKey == key) Angle += Settings.Radius * Settings.Speed;

            X += Convert.ToInt16(Math.Cos(Angle) * Settings.Speed);
            Y += Convert.ToInt16(Math.Sin(Angle) * Settings.Speed);
        }

        /// <summary>
        /// Method creates the stroke gaps
        /// </summary>
        /// <returns></returns>
        public DoubleCollection GetStrokeDashArray()
        {
            var strokeDashArray = new DoubleCollection();
            strokeDashArray.Add(randomgenerator.Next(1, 5) * (Angle > 180 ? 100 : (Angle < 50 ? 100: Angle)));
            strokeDashArray.Add(3);
            strokeDashArray.Add(randomgenerator.Next(1, 5) * (Angle > 180 ? 100 : (Angle < 50 ? 100 : Angle)));
            strokeDashArray.Add(5);

            return strokeDashArray;
        }
    }
}
